package com.seed.core;

import cn.hutool.core.util.StrUtil;
import com.seed.core.action.Action;
import com.seed.core.pojo.seed.Seed;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

public class Lib {
    protected String name;
    protected String label;
    protected String comment;

    protected Map<String, Seed> seeds;
    protected Map<String, Action> actions;

    public Action getAction(String seedName, RequestMethod method, String actionName) {
        if(StrUtil.isBlank(seedName)) return getAction(method, actionName);

        if(seeds == null) return null;

        Seed seed = seeds.get(seedName);
        if(seed == null) return null;

        Map<String, Action> actions = seed.getActions();
        if(actions == null) return null;

        Action action = actions.get(actionName);
        if(action == null || action.getMethods() == null) return null;

        return action.getMethods().contains(method) ? action : null;
    }

    public Action getAction(RequestMethod method, String actionName) {
        if(actions == null) return null;

        Action action = actions.get(actionName);
        if(action == null || action.getMethods() == null) return null;

        return action.getMethods().contains(method) ? action : null;
    }

    public String getName() {
        return name;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Map<String, Seed> getSeeds() {
        return seeds;
    }

    public Map<String, Action> getActions() {
        return actions;
    }
}
